package cuit.epoch.pymjl.security.common.utils;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import cuit.epoch.pymjl.security.common.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@ResponseBody
public class CaptchaController {
    String yz = null;
    @Resource
    private RedisServiceImpl redisClient;

    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        specCaptcha.setFont(Captcha.FONT_1);
        String id = UUID.randomUUID().toString();
        response.setHeader("id", id);
        CaptchaUtil.out(specCaptcha, request, response);
        String verCode = specCaptcha.text().toLowerCase();
        redisClient.set(id, verCode,500);//可设置有效时间，单位为秒
        System.out.println(id);
        this.yz=id;
    }

    @GetMapping("/check")
    public Result check(String id) {
        System.out.println(this.yz);
        String s = redisClient.get(this.yz).toString();//id
        return Result.suc(id.equalsIgnoreCase(s));
    }
}
